package javolution.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import javax.realtime.MemoryArea;
import javolution.lang.Realtime;
import javolution.text.Text;
import javolution.xml.XMLSerializable;

/* loaded from: classes2.dex */
public abstract class FastCollection<E> implements Collection<E>, XMLSerializable, Realtime {
    private FastCollection<E>.Unmodifiable _unmodifiable;

    /* loaded from: classes2.dex */
    public interface Record {
        Record getNext();

        Record getPrevious();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public final class Unmodifiable extends FastCollection implements Set, List {
        private Unmodifiable() {
        }

        @Override // java.util.List
        public void add(int i, Object obj) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean add(Object obj) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // java.util.List
        public boolean addAll(int i, Collection collection) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean contains(Object obj) {
            return FastCollection.this.contains(obj);
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public boolean containsAll(Collection collection) {
            return FastCollection.this.containsAll(collection);
        }

        @Override // javolution.util.FastCollection
        public void delete(Record record) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // java.util.List
        public Object get(int i) {
            return ((List) FastCollection.this).get(i);
        }

        @Override // javolution.util.FastCollection
        public FastComparator getValueComparator() {
            return FastCollection.this.getValueComparator();
        }

        @Override // javolution.util.FastCollection
        public Record head() {
            return FastCollection.this.head();
        }

        @Override // java.util.List
        public int indexOf(Object obj) {
            return ((List) FastCollection.this).indexOf(obj);
        }

        @Override // java.util.List
        public int lastIndexOf(Object obj) {
            return ((List) FastCollection.this).lastIndexOf(obj);
        }

        @Override // java.util.List
        public ListIterator listIterator() {
            throw new UnsupportedOperationException("List iterator not supported for unmodifiable collection");
        }

        @Override // java.util.List
        public ListIterator listIterator(int i) {
            throw new UnsupportedOperationException("List iterator not supported for unmodifiable collection");
        }

        @Override // java.util.List
        public Object remove(int i) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // java.util.List
        public Object set(int i, Object obj) {
            throw new UnsupportedOperationException("Unmodifiable");
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return FastCollection.this.size();
        }

        @Override // java.util.List
        public List subList(int i, int i2) {
            throw new UnsupportedOperationException("Sub-List not supported for unmodifiable collection");
        }

        @Override // javolution.util.FastCollection
        public Record tail() {
            return FastCollection.this.tail();
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(Record record) {
            return FastCollection.this.valueOf(record);
        }
    }

    private boolean addAll(FastCollection<? extends E> fastCollection) {
        if (fastCollection instanceof FastTable) {
            return addAll((FastTable) ((FastTable) fastCollection));
        }
        boolean z = false;
        Record head = fastCollection.head();
        Record tail = fastCollection.tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return z;
            }
            if (add(fastCollection.valueOf(head))) {
                z = true;
            }
        }
    }

    private boolean addAll(FastTable<? extends E> fastTable) {
        int size = fastTable.size();
        int i = 0;
        boolean z = false;
        while (i < size) {
            int i2 = i + 1;
            if (add(fastTable.get(i))) {
                z = true;
            }
            i = i2;
        }
        return z;
    }

    private boolean containsAll(FastCollection<?> fastCollection) {
        Record head = fastCollection.head();
        Record tail = fastCollection.tail();
        do {
            head = head.getNext();
            if (head == tail) {
                return true;
            }
        } while (contains(fastCollection.valueOf(head)));
        return false;
    }

    private boolean equalsList(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof List) {
            if (obj instanceof FastCollection) {
                return equalsList((FastCollection) obj);
            }
            List list = (List) obj;
            if (size() != list.size()) {
                return false;
            }
            Iterator<E> it = list.iterator();
            FastComparator<? super E> valueComparator = getValueComparator();
            Record head = head();
            Record tail = tail();
            do {
                head = head.getNext();
                if (head == tail) {
                    return true;
                }
            } while (valueComparator.areEqual((E) valueOf(head), (E) it.next()));
            return false;
        }
        return false;
    }

    private boolean equalsList(FastCollection fastCollection) {
        if (size() != fastCollection.size()) {
            return false;
        }
        Record head = fastCollection.head();
        FastComparator<? super E> valueComparator = getValueComparator();
        Record head2 = head();
        Record tail = tail();
        do {
            head2 = head2.getNext();
            if (head2 == tail) {
                return true;
            }
            head = head.getNext();
        } while (valueComparator.areEqual((E) valueOf(head2), (Object) fastCollection.valueOf(head)));
        return false;
    }

    private int hashCodeList() {
        FastComparator<? super E> valueComparator = getValueComparator();
        Record head = head();
        Record tail = tail();
        int i = 1;
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return i;
            }
            i = (i * 31) + valueComparator.hashCodeOf((E) valueOf(head));
        }
    }

    @Override // java.util.Collection
    public boolean add(E e) {
        throw new UnsupportedOperationException();
    }

    @Override // java.util.Collection
    public boolean addAll(Collection<? extends E> collection) {
        if (collection instanceof FastCollection) {
            return addAll((FastCollection) ((FastCollection) collection));
        }
        boolean z = false;
        Iterator<? extends E> it = collection.iterator();
        int size = collection.size();
        while (true) {
            size--;
            if (size < 0) {
                return z;
            }
            if (add(it.next())) {
                z = true;
            }
        }
    }

    @Override // java.util.Collection
    public void clear() {
        Record head = head();
        Record tail = tail();
        while (true) {
            tail = tail.getPrevious();
            if (tail == head) {
                return;
            }
            delete(tail);
        }
    }

    @Override // java.util.Collection
    public boolean contains(Object obj) {
        FastComparator<? super E> valueComparator = getValueComparator();
        Record head = head();
        Record tail = tail();
        do {
            head = head.getNext();
            if (head == tail) {
                return false;
            }
        } while (!valueComparator.areEqual(obj, (E) valueOf(head)));
        return true;
    }

    @Override // java.util.Collection
    public boolean containsAll(Collection<?> collection) {
        if (collection instanceof FastCollection) {
            return containsAll((FastCollection) collection);
        }
        Iterator<?> it = collection.iterator();
        int size = collection.size();
        do {
            size--;
            if (size < 0) {
                return true;
            }
        } while (contains(it.next()));
        return false;
    }

    public abstract void delete(Record record);

    @Override // java.util.Collection
    public boolean equals(Object obj) {
        if (this instanceof List) {
            return equalsList(obj);
        }
        if (obj != this) {
            if (obj instanceof Collection) {
                Collection<?> collection = (Collection) obj;
                if (collection.size() != size() || !containsAll(collection)) {
                }
            }
            return false;
        }
        return true;
    }

    public FastComparator<? super E> getValueComparator() {
        return FastComparator.DEFAULT;
    }

    @Override // java.util.Collection
    public int hashCode() {
        if (this instanceof List) {
            return hashCodeList();
        }
        FastComparator<? super E> valueComparator = getValueComparator();
        int i = 0;
        Record head = head();
        Record tail = tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return i;
            }
            i += valueComparator.hashCodeOf((E) valueOf(head));
        }
    }

    public abstract Record head();

    @Override // java.util.Collection
    public final boolean isEmpty() {
        return size() == 0;
    }

    @Override // java.util.Collection, java.lang.Iterable
    public Iterator<E> iterator() {
        return FastIterator.valueOf(this);
    }

    @Override // java.util.Collection
    public boolean remove(Object obj) {
        FastComparator<? super E> valueComparator = getValueComparator();
        Record head = head();
        Record tail = tail();
        do {
            head = head.getNext();
            if (head == tail) {
                return false;
            }
        } while (!valueComparator.areEqual(obj, (E) valueOf(head)));
        delete(head);
        return true;
    }

    @Override // java.util.Collection
    public boolean removeAll(Collection<?> collection) {
        Record head = head();
        Record previous = tail().getPrevious();
        boolean z = false;
        while (previous != head) {
            Record previous2 = previous.getPrevious();
            if (collection.contains(valueOf(previous))) {
                delete(previous);
                z = true;
            }
            previous = previous2;
        }
        return z;
    }

    @Override // java.util.Collection
    public boolean retainAll(Collection<?> collection) {
        Record head = head();
        Record previous = tail().getPrevious();
        boolean z = false;
        while (previous != head) {
            Record previous2 = previous.getPrevious();
            if (!collection.contains(valueOf(previous))) {
                delete(previous);
                z = true;
            }
            previous = previous2;
        }
        return z;
    }

    @Override // java.util.Collection
    public abstract int size();

    public abstract Record tail();

    @Override // java.util.Collection
    public Object[] toArray() {
        return toArray(new Object[size()]);
    }

    @Override // java.util.Collection
    public <T> T[] toArray(T[] tArr) {
        int size = size();
        if (tArr.length < size) {
            throw new UnsupportedOperationException("Destination array too small");
        }
        if (tArr.length > size) {
            tArr[size] = 0;
        }
        int i = 0;
        Record head = head();
        Record tail = tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return tArr;
            }
            tArr[i] = valueOf(head);
            i++;
        }
    }

    public final String toString() {
        return toText().toString();
    }

    @Override // javolution.lang.Realtime
    public Text toText() {
        Text valueOf = Text.valueOf((Object) "[");
        Record head = head();
        Record tail = tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return valueOf.plus("]");
            }
            valueOf = valueOf.plus(valueOf(head));
            if (head.getNext() != tail) {
                valueOf = valueOf.plus(", ");
            }
        }
    }

    public Collection<E> unmodifiable() {
        if (this._unmodifiable == null) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastCollection.1
                @Override // java.lang.Runnable
                public void run() {
                    FastCollection fastCollection = FastCollection.this;
                    fastCollection._unmodifiable = new Unmodifiable();
                }
            });
        }
        return this._unmodifiable;
    }

    public abstract E valueOf(Record record);
}
